package com.wind.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wind.dao.ProductDao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 服务器端分页
 * @author www.java1234.com
 *
 */
public class ServerPaginationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public ServerPaginationServlet() {
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductDao productDao=new ProductDao();
		// 获取页码
		int page=Integer.parseInt(request.getParameter("page"));
		// 获取每页多少行
		int rows=Integer.parseInt(request.getParameter("rows"));
		String sort=request.getParameter("sort");
		String order=request.getParameter("order");
		// 封装输出数据
		JSONObject resultJsonObject=new JSONObject();
		resultJsonObject.put("total", 100);
		resultJsonObject.put("rows", productDao.query(rows*(page-1), rows,sort,order));
		PrintWriter out = response.getWriter();
		out.print(resultJsonObject.toString());  
		out.flush();  
		out.close();  
	}

}
